package cn.kgm.autoCancel.quartz;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @Version 1.8
 * @Author: Kappi
 * @Date: 2023-9-15 11:13
 * @Description: cn.kgm.autoCancel.quartz.DelayActuator
 */
public class DelayActuator implements Delayed {

    private String orderId;

    private Long timeout;

    DelayActuator(String orderId, long timeout) {
        this.orderId = orderId;
        this.timeout = timeout + System.nanoTime();
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(timeout - System.nanoTime(), TimeUnit.NANOSECONDS);
    }

    @Override
    public int compareTo(Delayed other) {
        if (other == this) {
            return 0;
        }
        DelayActuator t = (DelayActuator) other;
        Long d = (getDelay(TimeUnit.NANOSECONDS) - t.getDelay(TimeUnit.NANOSECONDS));
        return (d == 0) ? 0 : ((d < 0) ? -1 : 1);
    }

    void print() {
        System.out.println(orderId + "要移除掉了");
    }

}
